package com.xpsoft.oa.workflow.event;

import java.math.BigDecimal;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jbpm.api.listener.EventListener;
import org.jbpm.api.listener.EventListenerExecution;
import org.jbpm.api.model.OpenProcessInstance;

import com.xpsoft.core.util.AppUtil;
import com.xpsoft.oa.model.flow.ProcessRun;
import com.xpsoft.oa.model.project.ProjectExpenses;
import com.xpsoft.oa.model.system.AppUser;
import com.xpsoft.oa.service.flow.ProcessRunService;
import com.xpsoft.oa.service.project.ProjectExpensesService;
import com.xpsoft.oa.service.project.ProjectService;

public class ProjectExpensesFinishListener implements EventListener{
	private Log logger=LogFactory.getLog(ProjectExpensesFinishListener.class);
	@Override
	public void notify(EventListenerExecution execution) throws Exception {
		//获取表单参数
		OpenProcessInstance pi=execution.getProcessInstance();
		String proId = (String)pi.getVariable("projectExpenses.project.id");
		Date formDate = (Date)pi.getVariable("projectExpenses.formDate");
		String expensesType = (String)pi.getVariable("projectExpenses.expensesType");
		String dataList = (String)pi.getVariable("projectExpenses.dataList");
//		BigDecimal amount = (BigDecimal)pi.getVariable("projectExpenses.amount");
//		String amountBig = (String)pi.getVariable("projectExpenses.amountBig");
		BigDecimal leanAmount = (BigDecimal)pi.getVariable("projectExpenses.leanAmount");
		String isToLean = (String)pi.getVariable("projectExpenses.isToLean");
		String remark = (String)pi.getVariable("projectExpenses.remark");
		
		ProjectExpenses projectExpenses = new ProjectExpenses();
		projectExpenses.setFormDate(formDate);
		projectExpenses.setExpensesType(expensesType);
//		projectExpenses.setAmount(amount);
//		projectExpenses.setAmountBig(amountBig);
		projectExpenses.setLeanAmount(leanAmount);
		projectExpenses.setIsToLean(isToLean);
		projectExpenses.setRemark(remark);
		
		if(StringUtils.isNotBlank(proId)){
			ProjectService projectService = (ProjectService)AppUtil.getBean("projectService");
			projectExpenses.setProject(projectService.get(Long.valueOf(proId)));
		}
		
		ProcessRunService processRunService=(ProcessRunService)AppUtil.getBean("processRunService");
		ProcessRun processRun=processRunService.getByExeId(pi.getId());
		projectExpenses.setProcessRunId(processRun.getRunId());
		
		ProjectExpensesService projectExpensesService = (ProjectExpensesService)AppUtil.getBean("projectExpensesService");
		projectExpensesService.saveProjectExpenses(projectExpenses,dataList);
		
		
		//获取流程发起者对象
		AppUser flowStartUser=(AppUser)execution.getProcessInstance().getVariable("flowStartUser");
	
		//所有参与流程的都发消息
		AppUtil.sendSysShortMsg(execution.getProcessInstance().getId(), flowStartUser);
	}

}
